package com.ym.shardingsphere.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.shardingsphere.dao.TxIntegralMapper;
import com.ym.shardingsphere.dao.TxOrderMapper;
import com.ym.shardingsphere.entity.TxIntegral;
import com.ym.shardingsphere.entity.TxOrder;
import com.ym.shardingsphere.service.ITxOrderService;
import com.ym.utils.*;
import com.ym.utils.constant.*;
import com.ym.utils.exception.*;
import com.ym.utils.uuid.*;
import com.ym.shardingsphere.vo.TxOrderVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @version v1.0
 * @ProjectName: management
 * @ClassName: 订单分库分表演示
 * @Description: TODO 订单分库分表演示 服务实现类
 * @Author: 晨曦
 * @Date: 2020-12-21 9:48:12
 */
@Service
public class TxOrderServiceImpl implements ITxOrderService {
    @Autowired
    private TxOrderMapper TxOrderMapper;

    @Autowired
    private TxIntegralMapper txIntegralMapper;

    /**
     * 订单分库分表演示添加
     *
     * @param param 添加入参
     * @return TxOrderVO
     * @throws CustomException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
   // @ShardingTransactionType(TransactionType.XA)  // 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE
    public TxOrderVO addTxOrder(TxOrderVO param) throws CustomException {
        if (ObjectUtils.isEmpty(param)) {
            throw new CustomException(MsgConstant.HttpStatus.BAD_REQUEST, MsgConstant.CEM.MISS_PARAM);
        }
        TxOrder record = new TxOrder()
                .setOrderId(new IdWorker(1, 1, 1).nextId())
                .setOrderNum(param.getOrderNum())
                .setOrderPrice(param.getOrderPrice())
                .setRemak(param.getRemak())
                .setCreatAt(new Date())
                .setUpdateAt(null)
                .setDbNum(param.getDbNum())
                .setDbTableNum(param.getDbTableNum());
        int count = TxOrderMapper.insertSelective(record);
        if (count == 0) {
            throw new CustomException(MsgConstant.HttpStatus.ERROR, MsgConstant.CEM.SAVE_FAIL);
        }
        /*TxIntegral recordTxIntegral = new TxIntegral()
                .setCreatAt(new Date())
                .setIntegralId(null)
                .setIntegralScores(new BigDecimal(1.1))
                .setLastUpdateAt(null);
        int count1 = txIntegralMapper.insert(recordTxIntegral);
        if (count1 == 0) {
            throw new CustomException(MsgConstant.HttpStatus.ERROR, MsgConstant.CEM.SAVE_FAIL);
        }*/
        //返回实体信息
        param.setOrderId(record.getOrderId());
        return param;
    }

    /**
     * 订单分库分表演示删除
     *
     * @param id 入参
     * @return void
     * @throws CustomException
     */
    @Override
    @Transactional
    @ShardingTransactionType(TransactionType.XA)  // 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE
    public void deleteTxOrderById(String id) throws CustomException {
        if (StringUtils.isBlank(id)) {
            throw new CustomException(MsgConstant.HttpStatus.BAD_REQUEST, MsgConstant.CEM.MISS_PARAM);
        }
        int count = TxOrderMapper.deleteByPrimaryKey(id);
        if (count == 0) {
            throw new CustomException(MsgConstant.HttpStatus.ERROR, MsgConstant.CEM.DELETE_FAIL);
        }
    }

    /**
     * 订单分库分表演示批量删除
     *
     * @param idList 入参
     * @return void
     * @throws CustomException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTxOrderByIds(List<String> idList) throws CustomException {
        if (ObjectUtils.isEmpty(idList)) {
            throw new CustomException(MsgConstant.HttpStatus.BAD_REQUEST, MsgConstant.CEM.MISS_PARAM);
        }
        int count = TxOrderMapper.deleteByIdList(idList);
        if (count == 0) {
            throw new CustomException(MsgConstant.HttpStatus.ERROR, MsgConstant.CEM.DELETE_FAIL);
        }
    }

    /**
     * 订单分库分表演示修改
     *
     * @param param 添加入参
     * @return TxOrderVO
     * @throws CustomException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public TxOrderVO updateTxOrder(TxOrderVO param) throws CustomException {
        if (ObjectUtils.isEmpty(param) || ObjectUtils.isEmpty(param.getOrderId())) {
            throw new CustomException(MsgConstant.HttpStatus.BAD_REQUEST, MsgConstant.CEM.MISS_PARAM);
        }
        TxOrder record = new TxOrder()
                .setOrderId(param.getOrderId())
                .setOrderNum(param.getOrderNum())
                .setOrderPrice(param.getOrderPrice())
                .setRemak(param.getRemak())
                .setCreatAt(param.getCreatAt())
                .setUpdateAt(param.getUpdateAt())
                .setDbNum(param.getDbNum());
        int count = TxOrderMapper.updateByPrimaryKeySelective(record);
        if (count == 0) {
            throw new CustomException(MsgConstant.HttpStatus.ERROR, MsgConstant.CEM.UPDATE_FAIL);
        }
        return param;
    }


    /**
     * 订单分库分表演示查询
     *
     * @param id 查询入参
     * @return TxOrder
     * @throws CustomException
     */
    @Override
    @Transactional(readOnly = true)
    public TxOrder getTxOrderInforById(String id) throws CustomException {
        TxOrder record = TxOrderMapper.selectByPrimaryKey(id);
        return record;
    }

    /**
     * 订单分库分表演示分页查询
     *
     * @param parmVO   查询入参
     * @param pageNum  当前页数
     * @param pageSize 分页条数
     * @return PageInfo
     * @throws CustomException
     */
    @Override
    @Transactional(readOnly = true)
    public PageInfo getTxOrderListByPage(TxOrderVO parmVO, Integer pageNum, Integer pageSize) throws CustomException {
        PageHelper.startPage(pageNum, pageSize);
        List<TxOrder> list = TxOrderMapper.getTxOrderListByPage(parmVO);
        PageInfo<TxOrder> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


    /**
     * 根据条件检索城市信息
     *
     * @param parmVO
     * @return
     * @throws CustomException
     */
    @Override
    @Transactional(readOnly = true)
    public List<TxOrder> getTxOrderListByParm(TxOrderVO parmVO) throws CustomException {
        List<TxOrder> list = TxOrderMapper.getTxOrderListByParm(parmVO);
        return list;
    }
}
